C# DataGirdView相關應用


假設視窗已經有一個dataGridView1,沒有的話也可以用下列方法新增

DataGridView dataGridView1 = new DataGridView();
this.Controls.Add(dataGridView1); // 新增到當前的 Form 中

填滿視窗

dataGridView.Dock = DockStyle.Fill; // 填滿視窗

依照內容大小調整欄寬

dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;

假設已有id、name兩個欄位
先逐筆新增資料

dataGridView1.Rows.Add(30, "Curry");
dataGridView1.Rows.Add(24, "Kobe");
dataGridView1.Rows.Add(23, "Lebron");

更改指定欄位值的方法
可以看到這兩個方法都是修改同一個欄位
不同的是一個用index(注意都是從0算起)
一個則是直接指定欄位name(注意不是指定欄首)

//擇一
dataGridView1.Rows[2].Cells[1].Value = "Durant";
dataGridView1.Rows[2].Cells["name"].Value = "Durant";

獲取當前選中行

int currentRow = dataGridView1.CurrentCell.RowIndex;

改變行背景顏色

dataGridView1.Rows[index].DefaultCellStyle.BackColor=Color.Gray;
#C# #Winform







你可能感興趣的文章

金魚系列,淺談如何學習 css

金魚系列,淺談如何學習 css

淺談 React 中的 State 與 Props

淺談 React 中的 State 與 Props

[第六週]  CSS  Part1 - Selector

[第六週] CSS Part1 - Selector






留言討論